package edu.ccut.saturn.studio.logic.parseutil;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.saturn.SaturnDbDataElement;

public class DbData {

	public static List<SaturnDbDataElement> getColumnNodeList(Element rootElement) {

		List<SaturnDbDataElement> saturnDbDataElements = new ArrayList<SaturnDbDataElement>();
		NodeList fieldNodes = rootElement.getElementsByTagName("field");
		for (int i = 0; i < fieldNodes.getLength(); i++) {
			saturnDbDataElements.add(parseTitndbdataElement(fieldNodes.item(i)));
		}
		return saturnDbDataElements;
	}

	/**
	 * 得到filed对应的SaturnDbDataElement
	 * 
	 * @param elementNode
	 * @return
	 */
	private static SaturnDbDataElement parseTitndbdataElement(Node elementNode) {
		SaturnDbDataElement saturnDbDataElement = new SaturnDbDataElement();
		if ("field".equals(elementNode.getNodeName())) {

			String name = elementNode.getAttributes().getNamedItem("name").getNodeValue();
			
			Node columnAlias = elementNode.getAttributes().getNamedItem("columnAlias");
			
			if (columnAlias == null || columnAlias.getNodeValue().equals("")) {
				saturnDbDataElement.setName(name);
			} else {
				saturnDbDataElement.setName(elementNode.getAttributes()
						.getNamedItem("columnAlias").getNodeValue());
			}
			
			String xmlJavaType = elementNode.getAttributes().getNamedItem("javaType").getNodeValue();
			saturnDbDataElement.setXmlJavaType(xmlJavaType);
		}
		return saturnDbDataElement;
	}
}
